#include "convertmainwindow.h"
#include "ui_convertmainwindow.h"

#include"convertprocess.h"

#include<QFileDialog>
#include<QString>
#include<QMessageBox>

convertMainWindow::convertMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::convertMainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(600,800);

    ui->progressBar->setVisible(false);
    ui->lb_fileName->setVisible(false);
    ui->progressBar->setValue(0);

    ui->progressBar->setRange(0,100);

    connect(ui->frame_center,&DropFrame::fileNameReady,this,&convertMainWindow::onFileNameReady);
}


convertMainWindow::~convertMainWindow()
{
    delete ui;
}

void convertMainWindow::addOutputCombo(size_t type)
{
    ui->comboBox->clear();
    if(type == AUDIO){
        for(size_t i =0;i<audio_Vec.size() ; ++i){
            ui->comboBox->addItem(audio_Vec[i]);
        }
    }
    else{
        for(size_t i =0;i<video_Vec.size() ; ++i){
            ui->comboBox->addItem(video_Vec[i]);
        }
    }
}

void convertMainWindow::setOutputCombo()
{
    size_t outputType;
    if(std::find(audio_Vec.begin(),audio_Vec.end(),input_Fmt)!=audio_Vec.end()){
        outputType = AUDIO;
    }
    else{
        outputType = VIDEO;
    }

    addOutputCombo(outputType);
}


void convertMainWindow::on_btn_file_clicked()
{

    QString fileName = QFileDialog::getOpenFileName(this, "选择文件",
                                                    "/home",
                                                    "视频文件 (*.mp4 *.ts *.mkv *.mov *.flv);;音频文件(*.mp3 *.aac *.wav)");

    if(fileName.isEmpty()){
        return;
    }

    input_FilePath = fileName.left(fileName.lastIndexOf("."));
    input_Fmt = fileName.section(".",-1);
    qDebug()<<input_FilePath;
    output_Fmt = input_Fmt;


    ui->lb_fileName->setText(fileName.section("/",-1));
    ui->lb_fileName->setVisible(true);
    ui->progressBar->setVisible(false);

    setOutputCombo();
}



void convertMainWindow::on_btn_convert_clicked()
{
    if(ui->lb_title->text() == "" || input_FilePath == ""){
        QMessageBox::warning(nullptr,"警告","请选择文件");
        return;
    }

    //选择文件夹路径
    QString defaultName = "save." + output_Fmt;
    QString saveFileType = "(*." + output_Fmt + ")";
    QString saveFileName = QFileDialog::getSaveFileName(this,"保存文件",defaultName,"文件类型" + saveFileType);

    if(saveFileName.isEmpty()){
        return;
    }

    qDebug()<<"name = "<<saveFileName;
    this->output_Name = saveFileName;
    ui->progressBar->setVisible(true);

    if(!m_convertProcess){
        m_convertProcess = new convertProcess(this);
        connect(m_convertProcess,&convertProcess::progressUpdated,this,&convertMainWindow::setProgressValue);
    }

    m_convertProcess->startProcess(input_FilePath,input_Fmt,output_Fmt,saveFileName.left(saveFileName.lastIndexOf(".")));



}


void convertMainWindow::on_comboBox_currentIndexChanged(const QString &out_Fmt)
{
    this->output_Fmt = out_Fmt;
}

void convertMainWindow::setProgressValue(double value)
{
    ui->progressBar->setValue(static_cast<int>(value + 0.5));
}

void convertMainWindow::onFileNameReady(QString const& fileName)
{

    input_FilePath = fileName.left(fileName.lastIndexOf("."));
    input_Fmt = fileName.section(".",-1);
    qDebug()<<input_FilePath;
    output_Fmt = input_Fmt;


    ui->lb_fileName->setText(fileName.section("/",-1));
    ui->lb_fileName->setVisible(true);
    ui->progressBar->setVisible(false);

    setOutputCombo();

}


void convertMainWindow::on_pushButton_clicked()
{
    if(m_convertProcess){
        m_convertProcess->stopProcess();
    }
}
